import { reactive, ref } from "vue";

// 单table页面 hook
export function tableHook(config: any) {
    // table 数据
    const tableData = ref([]);
    // table加载状态
    const loading = ref(false);
    //页码配置
    const pagination = reactive({
        current: 1,
        pageSize: 20,
        options: [10, 20, 50, 100],
        total: 0
    })
    /**
     * 分页数量变化，实际并未生效
     */
    const handleSizeChange = () => {
        try {
            config['queryList']()
        } catch (e) {
            //
        }
    }
    /**
     * 页码变化
     */
    const handleCurrentChange = () => {
        try {
            config['queryList']()
        } catch (e) {
            //
        }
    }

    /**
     * 搜索
     */
    function search() {
        pagination.current = 1
        try {
            config['queryList']()
        } catch (e) {
            //
        }
    }

    /**
     * 重置
     */
    const reset = () => {
        pagination.current = 1
        try {
            config['queryForm'].value = {}
            config['queryList']()
        } catch (e) {
            //
        }
    }
    return {
        tableData,
        loading,
        pagination,
        handleCurrentChange,
        handleSizeChange,
        reset,
        search,
    }
}
